home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 40
/
Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso
/
Aminet
/
comm
/
tcp
/
rxsocket.lha
/
rxsocket
/
Examples
/
ntblockecho.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1999-05-29
|
3KB
|
117 lines
/*
Name: ntblockecho.rexx
Version: 2.0
Author: alfie
Date: 28.04.98
Description: shows how to make a non blocking TCP connection;
the stopping event is a msg on port "PORT";
Usage: rx ntblockecho <host>
Note: it is a simple echo client in itself.
Be sure to have echo/tcp enabled in the service
and inetd database, before testing it on localhost.
Anyway, 'cause of a sort of delay is required to
test the stopping-event, try this macro on a very
slow non-local host with echo/tcp service.
*/
l="rexxsupport.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
l="rxsocket.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
prg = ProgramName("NOEXT")
if ~RMH_ReadArgs("HOST/A") then do
call PrintFault(IoErr(),prg)
exit
end
addr = resolve(parm.0.value)
if addr=="-1" then call err "no host <"parm.0.value">"
/*set the remote addr*/
sin.addrFamily = "INET"
sin.addrAddr = addr
/*try to find the echo service via GetServByName()*/
if GetServByName("SE","echo","tcp") then sin.addrPort = SE.servPort
else sin.addrPort = 7
/*open a port, a message on which will stop us*/
if ~OpenPort("PORT") then call err "can't open my port"
/*get the port signal mask */
portSig = PortSignal("PORT")
/*allocate a signal we want to be notified on for a socket events*/
sigBit=AllocSignal()
if sigBit==-1 then call err "can't allocate signal bit"
sig=2**sigBit
/*create the socket*/
sock = socket("INET","STREAM","IP")
if sock<0 then call err "can't create socket ("errno()")"
/*the socket must NOT block*/
call IOCtlSocket(sock,"FIONBIO",1)
/*notif us on connect*/
call SetSockOpt(sock,"SOCKET","EVENTMASK","CONNECT")
/*notify us via that signal we allocated*/
SET.SIGEVENTMASK = sig
call SetSocketBase("SET")
/*connect the echo service on the host*/
res = connect(sock,"SIN")
err=errno()
if (res<0) & (err~=35) & (err~=36) then call err "connect error ("errno()")"
/*we even set a timeout of 10 seconds*/
timeout = 10
signal = or(portSig,sig)
SEL.WRITE.0 = sock
con = 0
time = time("R")
do while ~con & (time("E")<timeout)
res = WaitSelect("SEL",timeout,0,signal)
if and(SEL.SIGNALS,portSig)~=0 then do
pkt = GetPkt("PORT")
if pkt~=null() then do
m = GetArg(pkt)
call reply(pkt)
say "message on PORT:" m
end
end
if res==1 then con = 1;
end
if ~con then call err "timeout"
say "--- connected ---"
call IOCtlSocket(sock,"FIONBIO",0)
request = "echo service test"
res = send(sock,request)
if res~=length(request) then call err "send errore ("errno()")"
len = recv(sock,"BUFF",256)
if len<0 then call err "recv error ("errno()")"
say buff
exit
err: procedure expose prg
parse arg msg
say prg":" msg
exit